package bamin.com.bamindaijia.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.widget.SingleBtnDialog;
import com.aiton.administrator.shane_library.shane.widget.TwoBtnDialog;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;
import bamin.com.bamindaijia.model.Order;
import bamin.com.bamindaijia.model.Sms;
import cz.msebera.android.httpclient.Header;

public class PingJiaActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText mEdt_pingjia;
    private TextView mTv_editNum;
    private ImageView mImg_touxian;
    private TextView mTv_driverYear;
    private TextView mTv_orderNum;
    private Button mBtn_sure;
    private TextView mTv_complain;

    private SimpleRatingBar mRatingBar;
    private String mode = "";
    private String mImage;
    private String driverName = "";
    private int driverOrderSum;
    private int driverId;
    private int orderId;
    private Order mDatas;
    private String mDriverDrivingTime;
    private double mDriverScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ping_jia);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {
//        if (mImage != null && !TextUtils.isEmpty(mImage)) {
//            ImageLoader.getInstance().clearDiskCache();
//            UILUtils.displayImageNoAnim(mImage, mImg_touxian);
//        }
//        mTv_driverName.setText("司机：" + driverName);
//        mTv_driverYear.setText("驾龄：5年");
//        mTv_orderNum.setText("接单数：" + driverOrderSum + "单");

    }

    @Override
    protected void setListeners() {
//        mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
//            @Override
//            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
//                Toast.makeText(PingJiaActivity.this, "" +rating , Toast.LENGTH_SHORT).show();
//            }
//        });
        findViewById(R.id.imageView_back).setOnClickListener(this);
        findViewById(R.id.textView_complain).setOnClickListener(this);
        mBtn_sure.setOnClickListener(this);

        mEdt_pingjia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                mTv_editNum.setText("" + s.length());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
//        mode = intent.getStringExtra(Constant.INTENT_KEY.PINGJIA);
        Log.e("PingJiaActivity", "initIntent: --->>" + mode);

        //判断是支付完成后进入还是在订单详情进入
        mDatas = (Order) intent.getSerializableExtra(Constant.INTENT_KEY.ORDER);

        if (mDatas != null) {

            mImage = mDatas.getDriverImage();
            driverName = mDatas.getDriverName();
            driverOrderSum = mDatas.getDriverOrderSum();
            driverId = mDatas.getDriverId();
            orderId = mDatas.getId();
            mDriverDrivingTime = mDatas.getDriverDrivingTime();
            mDriverScore = mDatas.getDriverScore();


            Log.e("PingJiaActivity", "PAY: --->>" + mImage);
            Log.e("PingJiaActivity", "PAY: --->>" + driverName);
            Log.e("PingJiaActivity", "PAY: --->>" + driverOrderSum);
            Log.e("PingJiaActivity", "PAY: --->>" + driverId);
            Log.e("PingJiaActivity", "PAY: --->>" + orderId);
        }
//
    }

    @Override
    protected void findID() {
        mRatingBar = (SimpleRatingBar) findViewById(R.id.ratingbar_pingfeng);
        mEdt_pingjia = (EditText) findViewById(R.id.edt_pingjia);
        mTv_editNum = (TextView) findViewById(R.id.tv_edittextNum);
//        mImg_touxian = (ImageView) findViewById(R.id.img_order_driverImage);
//        mTv_driverYear = (TextView) findViewById(R.id.textView_driveYear);
//        mTv_orderNum = (TextView) findViewById(R.id.textView_orderNum);
        mBtn_sure = (Button) findViewById(R.id.button_sure);
        mTv_complain = (TextView) findViewById(R.id.textView_complain);
//        mTv_driverName = (TextView) findViewById(R.id.textView_driverName);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.imageView_back:
                backDialog();
                break;
            case R.id.button_sure://确定
                pushPingjia();
                break;
            case R.id.textView_complain:

                intent.setClass(this, ComplaintActivity.class);
//                intent.putExtra(Constant.INTENT_KEY.DRIVER_INFO, mDriverInfo);
                intent.putExtra(Constant.INTENT_KEY.ORDER, mDatas);
                startActivityTo(intent);
                break;
        }
    }

    /**
     * 上传评价信息
     */
    private void pushPingjia() {
        showLoadingDialog();
        String url = Constant.Url.SET_PINGJIA;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("orderId", orderId);
        params.put("driverId", driverId);
        params.put("score", mRatingBar.getRating() + "");
        params.put("comment", mEdt_pingjia.getText().toString().trim());
        Log.e("PingJiaActivity", "score: --->>" + mRatingBar.getRating());
        Log.e("PingJiaActivity", "comment: --->>" + mEdt_pingjia.getText().toString().trim());
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                Log.e("PingJiaActivity", "评价返回" + s);
                try {
                    cancelLoadingDialog();
                    Sms sms = GsonUtils.parseJSON(s, Sms.class);
                    if (sms.getStatus() == 0) {

                        Toast.makeText(PingJiaActivity.this, sms.getMes(), Toast.LENGTH_SHORT).show();

                        final SingleBtnDialog singleBtnDialog = new SingleBtnDialog(PingJiaActivity.this, "评论上传成功", "确定");
                        singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {
                            @Override
                            public void doWhat() {
                                startActivityTo(new Intent(PingJiaActivity.this, MainActivity.class));
                                finishTo();
                                singleBtnDialog.dismiss();


                            }
                        });
                        singleBtnDialog.show();


                    } else {
                        Toast.makeText(PingJiaActivity.this, sms.getMes(), Toast.LENGTH_SHORT).show();

                    }
                } catch (Exception e) {
                    cancelLoadingDialog();

                    Toast.makeText(PingJiaActivity.this, "评价失败,请检查网络", Toast.LENGTH_SHORT).show();

                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e("PingJiaActivity", "请求失败");
                cancelLoadingDialog();

                Toast.makeText(PingJiaActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onBackPressed() {
        backDialog();
    }

    private void backDialog() {
        final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(this, "亲，评价还未完成，您确定要离开", "确定", "取消");
        twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                    startActivityTo(new Intent(PingJiaActivity.this, MainActivity.class));
                    finishTo();
                    twoBtnDialog.dismiss();
            }
            @Override
            public void doCancel() {
                twoBtnDialog.dismiss();
            }
        });
        twoBtnDialog.show();
    }

}
